package com.base.library.utils

import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import java.io.*
import javax.inject.Inject

class StorageUtil @Inject constructor(@ApplicationContext private val mContext: Context) {
    fun readAssetFile(filename: String): String? {
        return try {
            val input = mContext.assets.open(filename)
            readStringFromStream(input)
        } catch (e: IOException) {
            e.printStackTrace()
            null
        }
    }

    fun readSDFile(filePath: String): String? {
        return try {
            val input: InputStream = FileInputStream(filePath)
            val content = readStringFromStream(input)
            input.close()
            content
        } catch (e: IOException) {
            e.printStackTrace()
            null
        }
    }

    fun writeSDFile(filePath: String, content: ByteArray) {
        try {
            val targetFile = File(filePath)
            if (targetFile.parentFile?.exists() == false) {
                targetFile.parentFile?.mkdirs()
            }
            val output: OutputStream = FileOutputStream(targetFile)
            output.write(content)
            output.flush()
            output.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    @Throws(IOException::class)
    private fun readStringFromStream(input: InputStream): String {
        val result = StringBuilder()
        val buffer = ByteArray(1024)
        var read: Int
        while (input.read(buffer).also { read = it } > 0) {
            result.append(String(buffer, 0, read))
        }
        return result.toString()
    }

    fun copyAssetFolder(srcName: String, dstName: String): Boolean {
        return try {
            var result = true
            val fileList = mContext.assets.list(srcName) ?: return false
            if (fileList.isEmpty()) {
                result = copyAssetFile(srcName, dstName)
            } else {
                val file = File(dstName)
                result = file.mkdirs()
                for (filename in fileList) {
                    result = result and copyAssetFolder(
                        srcName + File.separator + filename,
                        dstName + File.separator + filename
                    )
                }
            }
            result
        } catch (e: IOException) {
            e.printStackTrace()
            false
        }
    }

    private fun copyAssetFile(srcName: String, dstName: String): Boolean {
        return try {
            val inputStream = mContext.assets.open(srcName)
            val outFile = File(dstName)
            if (outFile.parentFile?.exists() == false) {
                outFile.parentFile?.mkdirs()
            }
            val out: OutputStream = FileOutputStream(outFile)
            val buffer = ByteArray(1024)
            var read: Int
            while (inputStream.read(buffer).also { read = it } != -1) {
                out.write(buffer, 0, read)
            }
            inputStream.close()
            out.close()
            true
        } catch (e: IOException) {
            e.printStackTrace()
            false
        }
    }
}